package pers.mine.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import pers.mine.po.Member;
import pers.mine.po.MemberExample;
import pers.mine.service.inter.MemberService;
import pers.mine.vo.Info;
import pers.mine.vo.PageInfo;

/**
 * 后台会员账户Controller
 * 
 * @author Mine
 * @since 2016年10月31日_上午9:09:50
 */
@Controller
@RequestMapping("adminMember")
public class AdminMemberController {
	private String pageTitle = "会员管理";
	// 放到首页的信息,设置成全局变量>>>看起来好像有问题...
	private Info indexinfo = new Info();
	@Autowired
	MemberService memberService;

	/**
	 * 依据分页信息,关键字信息 获得会员列表,并展示到后台和会员页
	 * 
	 * @param nowPage
	 *            当前页号
	 * @param pageSize
	 *            页面大小
	 * @param pageCount
	 *            总页号
	 * @param keys
	 *            关键字
	 */
	@RequestMapping(value = "/show")
	public String show(Integer nowPage, Integer pageSize, Integer pageCount,
			String keys, HttpServletRequest req) {
		MemberExample exam = new MemberExample();
		/*
		 * System.out.println("nowPage:"+nowPage);
		 * System.out.println("pageSize:"+pageSize);
		 * System.out.println("keys:"+keys);
		 */
		// 页号非法性验证
		// System.out.println(pageCount);
		// System.out.println(nowPage);
		if (pageCount != null && nowPage != null && pageCount < nowPage) {
			nowPage = pageCount;// 当前页号非法
		}
		exam.initLimit(nowPage, pageSize);

		if (keys != null && !"".equals(keys.trim())) {
			// 关键字写回页面
			req.setAttribute("keys", keys);

			String keysPro = "%" + keys + "%";
			// 写死了。。。
			exam.or().andNameLike(keysPro);

		}
		// 排序方式
		exam.setOrderByClause("m_id,m_name");
		// System.out.println(exam.showBase());
		// 依据筛选条件获取
		PageInfo<Member> pi = memberService.findPageByExample(exam);
		// System.out.println(pi);

		if (req.getAttribute("indexinfo") != null) {
			indexinfo = (Info) req.getAttribute("indexinfo");
		}
		if (indexinfo == null) {
			indexinfo = new Info();
		}
		// 判断是否可以放入信息
		if (indexinfo.isCanChange()) {
			indexinfo.setType(Info.SUCCESS);
			if (keys != null && !"".equals(keys.trim())) {
				indexinfo.setMsg("[商品显示]:对于关键字[" + keys + "]查询会员成功...");
			} else {
				indexinfo.setMsg("[商品显示]:查询所有会员成功...");
			}
		} else {
			indexinfo.setCanChange(true);
		}
		req.setAttribute("pageTitle", pageTitle);
		req.setAttribute("indexinfo", indexinfo);
		req.setAttribute("memberPageInfo", pi);
		return "admin/member/memberPage";
	}

	@RequestMapping(value = "/editState")
	public String editBaseInfo(Integer mid, Integer mstate, Model model) {
		System.out.println(mid + "||" + mstate);
		indexinfo.setCanChange(false);
		if (mid != null && mstate != null && mid.intValue() >= 1000) {// 基本数据合法性验证
			if (memberService.setMemberState(mid, mstate)) {
				indexinfo.setType(Info.SUCCESS);
				indexinfo.setMsg("[会员状态修改操作]:修改#" + mid + "号用户状态成功...");
			} else {
				indexinfo.setType(Info.ERROR);
				indexinfo.setMsg("[会员状态修改操作]:操作失败,数据库错误...");
			}
		} else {
			indexinfo.setType(Info.ERROR);
			indexinfo.setMsg("[会员状态修改操作]:操作失败,错误的参数...");
		}

		model.addAttribute("indexinfo", indexinfo);
		return "forward:show";// 回到会员展示首页
	}

}
